package com.chinacoal.hr.levelimpl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinacoal.hr.levelapi.client.AttendanceInfoClient;
import com.chinacoal.hr.levelapi.client.response.Result;
import com.chinacoal.hr.levelapi.client.response.ResultGenerator;
import com.chinacoal.hr.levelapi.entity.AttendanceInfo;
import com.chinacoal.hr.levelapi.entity.InfoSyncVO;
import com.chinacoal.hr.levelimpl.service.AttendanceInfoService;
import com.chinacoal.hr.levelimpl.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-13 22:42
 * @description: 考勤信息Controller
 **/
@RestController
public class AttendanceInfoController implements AttendanceInfoClient {
    private Logger logger = LoggerFactory.getLogger(AttendanceInfoController.class);

    @Autowired
    private AttendanceInfoService attendanceInfoService;


    /**
     * 考勤信息查询
     *
     * @param page          :当前页数
     * @param limit         :每页条数
     * @param beginDate     :开始日期
     * @param endDate       :结束日期
     * @param empType       :员工类型
     * @param confirmStatus :考勤状态
     * @param empName       :员工姓名
     * @param queryRange    :查询范围（all、dept、person）
     * @return : Result
     * @author : YunTao.Li
     * @date : 2019/11/13 2019/11/13
     */
    public Result list(Long page,
                       Long limit, String orgCode,
                       String deptCode, String deptName,
                       String empName, String empCode,
                       String confirmStatus, String beginDate,
                       String endDate, String empType,
                       String queryRange) {

        // 构建查询条件
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("page", page);
        condition.put("limit", limit);
        //condition.put("limit", 1000);
        condition.put("orgCode", orgCode);
        condition.put("deptCode", deptCode);
        condition.put("deptName", deptName);
        condition.put("empName", empName);
        condition.put("empCode", empCode);
        condition.put("beginDate", beginDate);
        condition.put("endDate", endDate);
        condition.put("empType", empType);
        condition.put("confirmStatus", confirmStatus);
        condition.put("queryRange", queryRange);

        IPage<AttendanceInfo> recordList = null;
        try {
            recordList = attendanceInfoService.getAttendanceInfoPage(condition);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return ResultGenerator.genOkResult(recordList);
    }

    /**
     * 根据年月与empid获得某人的考勤异常信息列表
     *
     * @param yrmonth :
     * @param empCode : 员工编码
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/21 2019/11/21
     */
    public Result exceptions(Long page,
                             Long limit,
                             String yrmonth,
                             String empCode) {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("page", page);
        condition.put("limit", limit);
        condition.put("confirmStatus", "N"); //TODO:查异常 这里肯定都是N
        condition.put("yrmonth", yrmonth);
        condition.put("empCode", empCode);
//        condition.put("empId", "63880766707549A0E055000000000001"); //TODO:换成当前登陆人

        // 获得异常的考勤信息
        IPage<AttendanceInfo> recordList = attendanceInfoService.getUnusualAttendanceInfoPage(condition);
        return ResultGenerator.genOkResult(recordList);
    }

    /**
     * 新增考勤信息
     *
     * @param attendanceInfo :
     * @return : Result
     * @author : YunTao.Li
     * @date : 2019/11/14 2019/11/14
     */
    public Result add(AttendanceInfo attendanceInfo) {

        return ResultGenerator.genOkResult();
    }

    /**
     * @param attendanceInfo :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/14 2019/11/14
     */

    public Result update(AttendanceInfo attendanceInfo) {

        return ResultGenerator.genOkResult();
    }

    /**
     * 考勤信息同步
     *
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/20 2019/12/20
     */

    public Result sync() {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            String beginDateStr = DateUtil.getFirstDayInMonth(format);
            String endDateStr = DateUtil.getLastDayInMonth(format);

            //DEBUGGER
//             beginDateStr = "20200727";
//             endDateStr = "20200731";

            logger.info("同步考勤信息,同步日期范围:" + beginDateStr + " ~ " + endDateStr);
            long startTime = System.currentTimeMillis();
//            this.attendanceInfoService.concurrentSyncAttendanceInfo(beginDateStr, endDateStr, 3);
            this.attendanceInfoService.syncAttendanceInfo(beginDateStr, endDateStr);
            long endTime = System.currentTimeMillis();
            logger.info("同步考勤信息,同步日期范围:" + beginDateStr + " ~ " + endDateStr + " 程序运行时间：" + (endTime - startTime) + "ms");
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult();
    }

    /**
     * 考勤信息同步
     *
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/20 2019/12/20
     */
    public Result syncWithRange( InfoSyncVO infoSyncQueryData) {
        try {
            Date beginDate = infoSyncQueryData.getBeginDate();
            Date endDate = infoSyncQueryData.getEndDate();

            if (beginDate == null || endDate == null) {

                return ResultGenerator.genFailedResult("同步开始时间或结束时间为空");
            }

            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            String beginDateStr = format.format(beginDate);
            String endDateStr = format.format(endDate);

            logger.info("同步考勤信息,同步日期范围:" + beginDateStr + " ~ " + endDateStr);
            long startTime = System.currentTimeMillis();
            this.attendanceInfoService.syncAttendanceInfo(beginDateStr, endDateStr);
            long endTime = System.currentTimeMillis();
            logger.info("同步考勤信息,同步日期范围:" + beginDateStr + " ~ " + endDateStr + " 程序运行时间：" + (endTime - startTime) + "ms");
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult();
    }
}
